<?php  
class ControllerModulephanloai extends Controller {
	protected $phanloai_id = 0;
	
	protected function index() {
		$this->language->load('module/phanloai');
		
    	$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->load->model('catalog/phanloai');
		$this->load->model('tool/seo_url');
		
		if (isset($this->request->get['phanloai_id'])) {
			$this->phanloai_id = $this->request->get['phanloai_id'];
		}
		
		$this->data['phanloai'] = $this->getPhanloais(0);
												
		$this->id = 'phanloai';

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/phanloai.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/phanloai.tpl';
		} else {
			$this->template = 'default/template/module/phanloai.tpl';
		}
		
		$this->render();
  	}
	
	protected function getPhanloais($parent_id, $current_path = '') {
		$this->load->helper('image');
		$this->load->model('tool/seo_url');
		$phanloai_id = $this->phanloai_id;
		
		$output = '';
		
		$results = $this->model_catalog_phanloai->getPhanloais($parent_id);

		$i = 0;
		foreach ($results as $result) {
			if ($result['image']) {
				$image = $result['image'];
			} else {
				$image = 'no_image.jpg';
			}
			$images =  image_resize_fix($image, 30, 30);
			$new_path = $result['phanloai_id'];
			
			$children = '';
		
			$children = $this->getPhanloais($result['phanloai_id'], $new_path);
			
			if (!$current_path) {
				if($i >= 1) {
					$output .= '</ul></div></div>';
				}
				$output .= '<div class="box"><div class="top">' .$result['name'].'</div><div id="category" class="middle" style="padding:0px;"><ul>';
			} else {
			
			if ($phanloai_id == $result['phanloai_id']) {
				$output .= '<li><a href="' . $this->model_tool_seo_url->rewrite($this->url->http('product/phanloai&phanloai_id=' . $new_path))  . '" class="select"><img align="left" class="image_sidebar" src="' . $images . '"/>' . $result['name'] . '</a></li>';
			} else {
				$output .= '<li><a href="' . $this->model_tool_seo_url->rewrite($this->url->http('product/phanloai&phanloai_id=' . $new_path))  . '"><img align="left" class="image_sidebar" src="' . $images . '"/>' . $result['name'] . '</a></li>';
			}
			}
        	$output .= $children;
        
		$i++;
		}
		
		return $output;
	}		
}
?>